﻿using System;
using System.Collections.Generic;
using DevComponents.DotNetBar;
using WoSoft.PACS.Data.DataSourceData;
using WoSoft.PACS.Data.Models;
using WoSoft.PACS.Data.ModelsExt;
using WoSoft.PACS.Win.Forms.SubForms;
using WoSoft.PACS.Win.MyToolBars;

namespace WoSoft.PACS.Win.Forms
{
    /// <summary>
    /// 获取HIS病人信息
    /// </summary>
    public partial class FrmGetPatients : FrmBase
    {
        private string _currentDataSourceID = string.Empty;
        private string ywType = string.Empty;  //门诊 住院 体检 其他
        private DataSourceModel _selectDataSource;
        private DataSourceService dss = new DataSourceService();


        public FrmGetPatients()
        {
            InitializeComponent();
        }

        public FrmGetPatients(RibbonBar ribar)
            : base(ribar)
        {
            InitializeComponent();
        }

        public override List<MyToolBars.OperationModel> Operations()
        {
            List<OperationModel> functionInfos = new List<OperationModel>();

            //配置数据源
            var dataSourceConfig = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "配置数据源",
                Image = global::WoSoft.PACS.Win.Properties.Resources.data_gear,
                ClickEvent = SelectDataSource
            };
            functionInfos.Add(dataSourceConfig);

            //查询数据
            var queryButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "查询数据",
                Image = global::WoSoft.PACS.Win.Properties.Resources.table_sql_run,
                ClickEvent = QuerySqlClick
            };
            functionInfos.Add(queryButton);

            //保存SQL
            var saveButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "保存SQL",
                Image = global::WoSoft.PACS.Win.Properties.Resources.disk_blue,
                ClickEvent = SaveDataSql
            };
            functionInfos.Add(saveButton);

            //刷新
            var refreshButton = new OperationModel()
            {
                ID = Guid.NewGuid().ToString(),
                Name = "刷新配置",
                Image = global::WoSoft.PACS.Win.Properties.Resources.refresh,
            };
            functionInfos.Add(refreshButton);
            return functionInfos;
        }

        //选择数据源
        private void SelectDataSource(object sender, EventArgs e)
        {
            FrmDataSource datasource = new FrmDataSource(_selectDataSource);
            
            var r = datasource.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                _selectDataSource = datasource.SelectDataSource;
            }
        }

        //保存数据源
        private void SaveDataSql(object sender, EventArgs e)
        {
            //保存SQL语句
            if (_selectDataSource != null)
            {
                var cnt = dss.SaveDataSql(this.richTextBoxEx1.Text, _selectDataSource.ID, ywType);
                if (cnt > 0)
                {
                    MessageBoxEx.Show("脚本保存成功.");
                }
            }
            else
            {
                MessageBoxEx.Show("请选择数据源后再保存脚本.");
            }
        }

        //加载数据
        private void FrmGetPatients_Load(object sender, EventArgs e)
        {
            //LoadDataSql();
            cbYwType.SelectedIndexChanged += new EventHandler(cbYwType_SelectedIndexChanged);
        }

        void cbYwType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ywType = ((DevComponents.Editors.ComboItem)this.cbYwType.SelectedItem).Value.ToString();
            _selectDataSource = dss.GetModelByID(ywType);
            //加载数据
            this.richTextBoxEx1.Text = _selectDataSource.SqlText;
        }

        //执行SQL查询
        private void QuerySqlClick(object sender, EventArgs e)
        {
            try
            {
                if (_selectDataSource == null)
                {
                    MessageBoxEx.Show("请选择执行的数据源");
                    return;
                }
                var dt = _selectDataSource.QueryTable(this.richTextBoxEx1.Text);
                this.dataGridViewX1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message);
                this.dataGridViewX1.DataSource = null;
            }
        }
    }
}
